#!/bin/sh
###############################################
# Script de Instalacao do LinuxStok e PHP-GTK2
# feito por Eduardo RBS
# versao 0.1
# ultima alteracao: 06/11/2006
# licenca GPL
#
#

# digite 0 para baixar do site, 1 para baixar via subversion, e 2 gerar pacote para local
SUBVERSION='0'
# pasta temporaria
TMP='/tmp'
# usuario atual
USUARIO=`whoami`
# nome do pacote a baixar/gerar
PACOTE="linuxstok-0.7.0rc1.tar.gz"
# nome da pasta a instalar em /usr/local/
PASTA="linuxstok"
# raiz da pasta appliacations do menu desktop
XDG_DATA_DIRS=$XDG_DATA_DIRS
# arquivo desktop
FILE_DESKTOP="/usr/local/$PASTA/util/linuxstok.desktop"


if test -z $XDG_DATA_DIRS ; then
    XDG_DATA_DIRS="/usr/share" ;
fi
# utilitario de mensagens ao usuario
if test -e "/usr/bin/zenity" ; then
    MOUSE="Clique em OK e"
    XMSG="/usr/bin/zenity --info --text " ;    
elif test -e "/usr/bin/xmessage"; then
    MOUSE="Clique em OK e"
    XMSG="/usr/bin/xmessage -center "
else
    XMSG="echo -e " 
fi
# distribuicao
if test -e /etc/debian_version; then
    DIST='debian';
elif test -e /etc/redhat-release; then
    DIST='redhat';    
fi

# teste do wget
if ! test -e "/usr/bin/wget" -o -e "/usr/local/bin/wget" ; then
    $XMSG "A instalacao do LinuxStok precisa do WGET. Instale-o."
    sleep 3
    exit ;
fi

# testando usuario
if test $USUARIO != "root" ; then
    $XMSG "\nVoce DEVE executar este script como usuario root!!!\n"
    if test $DIST == "debian"; then
        $XMSG "Para executar abra o script com 'gksu' " ;
    else
        $XMSG "Abra um terminal de comando\nVa para a pasta que voce baixou o script (talvez com 'cd Desktop')\nDigite 'su', pressione ENTER  e depois entre com a senha do root\nEm seguida digite 'sh lsinstall.sh' e pressione ENTER " ;
    fi
    $XMSG "O LinuxStok NAO foi instalado\n"
    sleep 3
    exit;    
fi
#cria pasta temporaria
if ! test -d $TMP; then
    mkdir $TMP ;
fi
# cria pasta raiz de instalacao
if ! test -d /usr/local ; then
    mkdir /usr/local ;
fi

if ! test -e /usr/local/php-gtk2/bin/php -a -d /usr/local/php-gtk2 -a -e /usr; then
    $XMSG "$MOUSE aguarde o download do PHP-GTK2"
    echo "baixando PHP-GTK2"
    cd $TMP
    # rm php-gtk2.tar.gz -f
    wget -c http://ufpr.dl.sourceforge.net/sourceforge/agata/php-gtk2.tar.gz
    
    $XMSG "$MOUSE aguarde a instalacao do PHP-GTK2"
    echo "instalando PHP-GTK2"
    tar -xzf php-gtk2.tar.gz -C/usr/local
    cd /usr/local
    chmod 777 php-gtk2 -Rf
    ln -sf /usr/local/php-gtk2/bin/php /usr/bin/php-gtk2
    ln -sf /usr/local/php-gtk2/bin/php /usr/local/bin/php-gtk2
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/php-gtk2/lib 
    #if test $DIST == 'debian' ; then
    #    apt-get install -y libltdl3 ;
    #fi ;    
fi

$XMSG "$MOUSE aguarde o download do LinuxStok"
echo "baixando LinuxStok"
cd $TMP
#rm $PACOTE -f
if test $SUBVERSION == '1' -a -e /usr/bin/svn ; then
    svn co -q https://svn.sourceforge.net/svnroot/linuxstok linuxstok
    tar -czf $PACOTE linuxstok ;
elif test $SUBVERSION == '2'; then
    cd /home/eduardo/workspace2
    rm $PACOTE -f
    cp linuxstok/. $PASTA -r -f
    tar -czf $PACOTE $PASTA ;
else
    wget -c http://osdn.dl.sourceforge.net/sourceforge/linuxstok/linuxstok-0.7.0rc1.tar.gz
fi

$XMSG "$MOUSE aguarde a instalacao do LinuxStok"
echo "instalando LinuxStok"
if test -e /usr/local/$PASTA; then
    SEGUNDOS=`date +%s`
    mv /usr/local/$PASTA /usr/local/$PASTA.old-$SEGUNDOS -f
    $XMSG "Pasta antiga do LinuxStok foi movida para\n/usr/local/$PASTA.old-$SEGUNDOS"
fi
tar -xzf $PACOTE -C/usr/local
cd /usr/local
chmod 777 linuxstok -Rf
cd $PASTA/DBDriver
#chmod 666 .config.db linuxstok.db
#if test -e /usr/bin/linuxstok ; then
rm /usr/bin/linuxstok -f ;
#fi
ln -s /usr/local/$PASTA/startls /usr/bin/linuxstok
cp FILE_DESKTOP $XDG_DATA_DIRS/applications/ -f

# add to all users' desktops (copied from turboprint)
for i in /home/*; do
   USER=${i##*/}
   if [ -e $i/KDesktop ] ; then
      cp FILE_DESKTOP $i/KDesktop/linuxstok.desktop
      chown $USER $i/KDesktop/linuxstok.desktop
   fi
   if [ -e $i/Desktop ] ; then
      cp FILE_DESKTOP $i/Desktop/linuxstok.desktop
      chown $USER $i/Desktop/linuxstok.desktop
   fi
done


$XMSG "Instalacao Finalizada!!!\nPara iniciar digite 'linuxstok' na linha de comando ou use os icones"

#retorna
#cd /home/eduardo/workspace2/linuxstok/util/
exit 
